<template>
  <el-dialog :visible.sync="isShow" width="800px" :title='title' append-to-body>
    <el-descriptions class="margin-top" :column="3" border>
      <el-descriptions-item label="用户ID" :labelStyle="{width: '100px'}">{{ rowData.code }}</el-descriptions-item>
      <el-descriptions-item label="用户昵称：" :labelStyle="{width: '100px'}">{{ rowData.nickname }}</el-descriptions-item>
      <el-descriptions-item label="用户手机号" :labelStyle="{width: '100px'}">{{ rowData.phone }}</el-descriptions-item>
      <el-descriptions-item label="当前积分" :labelStyle="{width: '100px'}">{{ currentMoney }}</el-descriptions-item>
    </el-descriptions>
    <el-form ref="rowForm" :model="rowForm" :rules="rules" label-position="top" style="margin-top: 20px;">
      <el-row style="display: flex;flex-direction: row;flex-wrap: wrap;">
        <el-col style="margin-left: 20px" :span="5">
          <el-form-item :label="title" prop="money">
            <el-input v-model="rowForm.money" placeholder="请输入" min="1" type="number" />
          </el-form-item>
        </el-col>
        <el-col style="margin-left: 20px" :span="24">
          <el-form-item label="备注：" prop="remark">
            <el-input v-model="rowForm.remark" placeholder="请输入" type="textarea" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="margin-top: 30px;width: 100%;display: flex;flex-direction: row;justify-content: flex-end;">
      <el-button type="primary" @click="submitForm('rowForm')">保存</el-button>
      <el-button @click="close" type="info" size="mini">关闭</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { Loading } from 'element-ui';
import { getUserInfoById, updateUserForAddIncome, updateUserForSubIncome } from "@/api/system/user.js";
export default {
  components: {
  },
  props: {
  },
  data() {
    return {
      title: '',
      key: '',
      currentMoney: 0,
      isShow: false,
      fullscreenLoading: null,
      rowId: '',
      rowForm: {},
      rowData: {},
      rules: {
          money: [{ required: true, message: '请输入', trigger: 'blur' }],
          remark: [{ required: true, message: '请输入', trigger: 'blur' }],
      },

    }
  },
  created() {

  },
  methods: {
    show(val) {
      this.isShow = true
      if (val && val.rowId) {
        this.rowId = val.rowId
        this.title = val.title
        this.key = val.key
        this.currentMoney = val.currentMoney
        this.rowForm = {}
        this.getItemInfo()
      } else {
        this.rowId = ''
        this.rowForm = {}
      }
    },
    close() {
      this.isShow = false
    },
    getItemInfo() {
      var that = this
      getUserInfoById({
        obj_id: this.rowId
      }).then(res => {
        if (res.data.code == 10000) {
          this.rowData = res.data.data
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.fullscreenLoading = Loading.service({ fullscreen: true, text: '正在保存，请稍后！' });
          if (this.key == 'addIncomes') {
            this.rowForm.rowData = parseInt(this.rowForm.money)
            updateUserForAddIncome({
              payway: 'incomes',
              userId: this.rowData.id,
              money: this.rowForm.money,
              remark: this.rowForm.remark,
            }).then(res => {
              this.fullscreenLoading.close()
              if (res.data.code == 10000) {
                this.close()
                this.$emit("confirm")
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          } else if (this.key == 'subIncomes'){
            updateUserForSubIncome({
              payway: 'incomes',
              userId: this.rowData.id,
              money: this.rowForm.money,
              remark: this.rowForm.remark,
            }).then(res => {
              this.fullscreenLoading.close()
              if (res.data.code == 10000) {
                this.close()
                this.$emit("confirm")
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          }else if (this.key == 'addBalance'){
            updateUserForAddIncome({
              payway: 'balance',
              userId: this.rowData.id,
              money: this.rowForm.money,
              remark: this.rowForm.remark,
            }).then(res => {
              this.fullscreenLoading.close()
              if (res.data.code == 10000) {
                this.close()
                this.$emit("confirm")
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          }else if (this.key == 'subBalance'){
            updateUserForSubIncome({
              payway: 'balance',
              userId: this.rowData.id,
              money: this.rowForm.money,
              remark: this.rowForm.remark,
            }).then(res => {
              this.fullscreenLoading.close()
              if (res.data.code == 10000) {
                this.close()
                this.$emit("confirm")
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          }else if (this.key == 'addIncomesOriginal'){
            updateUserForAddIncome({
              payway: 'incomesOriginal',
              userId: this.rowData.id,
              money: this.rowForm.money,
              remark: this.rowForm.remark,
            }).then(res => {
              this.fullscreenLoading.close()
              if (res.data.code == 10000) {
                this.close()
                this.$emit("confirm")
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          }else if (this.key == 'addIncomesReduction'){
            updateUserForAddIncome({
              payway: 'incomesReduction',
              userId: this.rowData.id,
              money: this.rowForm.money,
              remark: this.rowForm.remark,
            }).then(res => {
              this.fullscreenLoading.close()
              if (res.data.code == 10000) {
                this.close()
                this.$emit("confirm")
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          }

        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>